package com.homewaimai.framework.acl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;

public class DatasourceBeanLocator implements ApplicationContextAware, InitializingBean {

    private static ApplicationContext applicationContext;

    //
    private static boolean            initialized = false;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DatasourceBeanLocator.applicationContext = applicationContext;
        this.initialized = true;
    }

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(DatasourceBeanLocator.applicationContext, "BeanLocator not initialized properly yet");
    }

    public static Object getBean(String name) {
        return DatasourceBeanLocator.applicationContext.getBean(name);
    }

    public static boolean isInitialized() {
        return initialized;
    }

    public static javax.sql.DataSource getAclDatasource() {
        return (javax.sql.DataSource) DatasourceBeanLocator.applicationContext.getBean("acldataSource");
    }

    public static javax.sql.DataSource getAppDatasource() {
        return (javax.sql.DataSource) DatasourceBeanLocator.applicationContext.getBean("dataSource");
    }
}
